/**
 * 
 */
package eu.jakubiak.mcc.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.jakubiak.mcc.enums.Status;
import eu.jakubiak.mcc.protocol.Protocol;
import eu.jakubiak.mcc.request.IRequest;

/**
 * @author Antek
 * 
 */
public class Request implements IRequest {

	/**
	 * Logger
	 */
	private static final Logger logger = LoggerFactory.getLogger(Request.class);

	/** 
	 * Klient HTTP
	 */
	private HttpClient client;
	
	/**
	 * Protocol implementation
	 */
	private Protocol protocol;

	public Protocol getProtocol() {
		return protocol;
	}

	public void setProtocol(Protocol protocol) {
		this.protocol = protocol;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.jakubiak.mcc.request.IRequest#begin(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String)
	 */
	public String begin(String fileUrl, String format, String quality,
			String statusUrl) {
		String url = protocol.createUrlForBeginConversion(fileUrl, format,
				quality, statusUrl);
		logger.trace("Sending begin request: {}", url);
		GetMethod method = new GetMethod(url);
		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				logger.warn("Sending begin request failed {}", method.getStatusLine());
				return null;
			}
			String responseString = method.getResponseBodyAsString();
	        String key = protocol.parseBeginResponseAndGetKey(responseString);
	        return key;
		} catch(IOException e) {
			logger.warn("Error executing begin method", e);
		} finally {
			method.releaseConnection();
		}
        return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.jakubiak.mcc.request.IRequest#getResult(java.lang.String)
	 */
	public byte[] getResult(String key) {
		String url = protocol.createUrlForResult(key);
		logger.trace("Sending result request: {}", url);
		GetMethod method = new GetMethod(url);
		try {
			HttpClient client = new HttpClient();
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				logger.warn("Sending result request failed {}", method.getStatusLine());
				return null;
			}
			return method.getResponseBody();
		} catch(IOException e) {
			logger.warn("Error executing result method", e);
		} finally {
			method.releaseConnection();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.jakubiak.mcc.request.IRequest#getStatus(java.lang.String)
	 */
	public Status getStatus(String key) {
		String url = protocol.createUrlForStatus(key);
		logger.trace("Sending status request: {}", url);
		GetMethod method = new GetMethod(url);
		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				logger.warn("Sending begin status failed {}", method.getStatusLine());
				return null;
			}
			String responseString = method.getResponseBodyAsString();
	        Status status = protocol.parseStatusResponseAndGetStatus(responseString);
	        return status;
		} catch(IOException e) {
			logger.warn("Error executing status method", e);
		} finally {
			method.releaseConnection();
		}
        return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.jakubiak.mcc.request.IRequest#getThumb(java.lang.String)
	 */
	public byte[] getThumb(String key) {
		String url = protocol.createUrlForThumb(key);
		logger.trace("Sending thumb request: {}", url);
		GetMethod method = new GetMethod(url);
		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				logger.warn("Sending thumb request failed {}", method.getStatusLine());
				return null;
			}
			return method.getResponseBody();
		} catch(IOException e) {
			logger.warn("Error executing thumb method", e);
		} finally {
			method.releaseConnection();
		}
        return null;
	}

	
    /**
     * CRON
     */
    public String cron() {
        String url = protocol.getRootUrl() + "/cron";
        logger.trace("Sending cron request: {}", url);
		GetMethod method = new GetMethod(url);
		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				logger.warn("Sending thumb request failed {}", method.getStatusLine());
				return null;
			}
			return method.getResponseBodyAsString();
		} catch(IOException e) {
			logger.warn("Error executing thumb method", e);
		} finally {
			method.releaseConnection();
		}
        return null;
    }

    /**
     * Domyślny konstruktor
     */
	public Request() {
		super();
		client = new HttpClient();
	}
	
}
